package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.dto.CourceDto;
import com.qf.pojo.TbCource;
import com.qf.pojo.TbStudent;
import com.qf.result.R;
import com.qf.service.CourceService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * created by 赵文韬 on 2022/5/27 16:05
 */
@RestController
@RequestMapping("/course")
public class CourceController {
    @Resource
    private CourceService courceService;
    //根据课程名进行分页查询
    @GetMapping("/pageinfo")
    public R findAllByPage(@RequestParam(defaultValue = "1",name = "page") int pageNum, @RequestParam(defaultValue = "1",name = "limit")  int pageSize, String categoryName) {
        PageInfo<TbCource> pageInfo = courceService.findByPage(pageNum, pageSize, categoryName);
        return R.ok().setResult(pageInfo);
    }
    //修改课程
    @RequestMapping("/update")
    public R updateCource(CourceDto courceDto){
        int i = courceService.updateCource(courceDto);
        if (i!=0){
            return R.ok().setMsg("更新成功");
        }else {
            return R.error().setMsg("更新失败");
        }
    }
    //添加课程
    @RequestMapping("/add")
    public R addCource(CourceDto courceDto){
        int i = courceService.insertCource(courceDto);
        if (i==0){
           return R.error().setMsg("添加课程失败");
        }else {
            return R.ok().setMsg("添加课程成功");
        }
    }
    //删除课程
    @RequestMapping("/delete")
    public R delCource(int courceID){
        int i = courceService.deleteCource(courceID);
        if (i==0){
            return R.error().setMsg("删除失败");
        }else {
            return R.ok().setMsg("删除课程成功");
        }
    }
    //根据课程id管理课程
    @RequestMapping("/management")
    public R managementCource(int id){
        List<TbStudent> tbStudents = courceService.selectById(id);
        return R.ok().setResult(tbStudents);
    }
}
